# Contributor: Leonardo Arena <rnalrd@alpinelinux.org>
# Contributor: Francesco Colista <fcolista@alpinelinux.org>
# Maintainer: Francesco Colista <fcolista@alpinelinux.org>
pkgname=gvmd
pkgver=23.1.0
#PostgreSQL version
_pgver=16
# match postgres clang
_llvmver=15
pkgrel=0
pkgdesc="Greenbone Vulnerability Manager"
url="https://www.openvas.org/"
arch="all"
license="AGPL-3.0"
pkgusers="gvm"
pkggroups="gvm"
depends="rsync postgresql$_pgver postgresql$_pgver-contrib libxslt
sed gnutls-utils gvm-libs perl-xml-twig socat xmlstarlet zip"
makedepends="cmake gvm-libs-dev clang$_llvmver-dev glib-dev
	gpgme-dev libgcrypt-dev graphviz libical-dev
	postgresql$_pgver-dev gnutls-dev samurai
	doxygen xmltoman perl-sql-translator libbsd-dev
	bsd-compat-headers"
checkdepends="cppcheck"
subpackages="$pkgname-doc $pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://github.com/greenbone/gvmd/archive/v$pkgver.tar.gz
	$pkgname.initd
	$pkgname.logrotate

	greenbone-certdata-sync.conf
	greenbone-scapdata-sync.conf
	greenbone-nvt-sync.conf

	malloc-trim.patch
	disable-execinfo.patch
	"

build() {
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DPostgreSQL_TYPE_INCLUDE_DIR=/usr/include/postgresql/$_pgver/ \
		-DPostgreSQL_INCLUDE_DIRS=/usr/include/postgresql/$_pgver/ \
		-DSBINDIR=/usr/bin \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DSYSCONF_INSTALL_DIR=/etc \
		-DLOCALSTATEDIR=/var \
		-DGVMD_RUN_DIR=/run/gvmd \
		-DGVM_FEED_LOCK_PATH=/var/lib/gvm/feed-update.lock \
		-DOPENVAS_DEFAULT_SOCKET=/run/ospd/ospd-openvas.sock \
		-DLOGROTATE_DIR=/etc/logrotate.d
	cmake --build build
}

check() {
	cd build
	ninja check
}

package() {
	DESTDIR="$pkgdir" cmake --install build
	install -Dm644 "$srcdir"/$pkgname.logrotate "$pkgdir"/etc/logrotate.d/$pkgname
	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname

	for f in nvt certdata scapdata; do
		install -Dm755 "$srcdir"/greenbone-$f-sync.conf "$pkgdir"/etc/gvm/greenbone-$f-sync.conf
	done

	install -d -o $pkgusers -g $pkggroups "$pkgdir"/run/gvmd
	chown -R "$pkgusers":"$pkggroups" "$pkgdir"/run/gvmd

	install -d -o "$pkgusers" -g "$pkggroups" "$pkgdir"/var/lib/gvm/gvmd/gnupg

	install -d -o $pkgusers -g $pkggroups "$pkgdir"/var/log/gvm
	chown -R "$pkgusers":"$pkggroups" "$pkgdir"/var/log/gvm
	chmod -R g+srw "$pkgdir"/var/log/gvm

	install -d -o $pkgusers -g $pkggroups "$pkgdir"/var/lib/gvm
	chown -R "$pkgusers":"$pkggroups" "$pkgdir"/var/lib/gvm
	chmod -R g+srw "$pkgdir"/var/lib/gvm
}

doc() {
	default_doc
	mkdir -p "$subpkgdir"/usr/share/doc/$pkgname
	cat >"$subpkgdir"/usr/share/doc/$pkgname/README.alpine <<EOF
	** Add texlive to generate PDF reports with
	** apk add texlive
EOF
}

sha512sums="
77d08067a168065bb3b4fd10ba94c48a3207378c9b8c0be42a9570c390c92b985ebf94c53cbbe04f78c2a802de430d599593e1971bc7b93d3f0652bfe59022a4  gvmd-23.1.0.tar.gz
02c8acb54a765d840747156a58a126bdc2d2c7f587c71109d83404100a5b81300ca68ed8b2f7e4637581fa9bbe3016809c70f1dc5fc5099a2f173ddb0299a72f  gvmd.initd
7c5520be5d2efdb6f9b6a4d80d00accd45d6663c184b2200f84d9394d11782f17b273037c8a42c1be8508b86c4478db0e634bf03e86054f45af70d77690f80b3  gvmd.logrotate
4fda7d5051f9d1ca069becbcc0e82b20d76fd9f7c7abf4a4f05ab55c17fba54dbe269607ee6360b034e8a1674a38680775e2ffc4120b12c7b679564ba4c87ed4  greenbone-certdata-sync.conf
1481eeb4182109295344768465a2db3895f51d39d6d2dee9a5db20db2ac53aec4df50eb2d5823c5a8d1f0897e284ad42c17ab59e3bef77614db6195dfbc5ae05  greenbone-scapdata-sync.conf
0734c6dc3ba065daeaeeb80aa23b52b141ff1056bffaa62a7eb6be5acf559a287354df418a40f804269a76644dbdc0f8c0229b380954628d6842168860bd344a  greenbone-nvt-sync.conf
73e1d455573dc4f11e86904740ea5ea7163d76fd422b81978f2a2a7127a8b50f6a1ad5f1f259fb86469d4d3ce5754db549158477696bb92b2b783d19b7bdbc63  malloc-trim.patch
43a164ecd249bb96a4a2f145eac0e6eb114247c8af299eaeb21e16e258e03d69711a3c6a9e6f39b9c531f4c68722c256da9cc18419e11ccf8f7d6efd14a44c7a  disable-execinfo.patch
"
